package com.example.booksystem.dao;

import com.example.booksystem.bean.MemberType;
import com.example.booksystem.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MemberTypeDao {
    //查询所有的会员类型
    public List<MemberType> getAll(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<MemberType> list = new ArrayList<>();

        try {
            connection = DBUtil.getConnection();
            String sql="select * from membertype";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                MemberType memberType = new MemberType(resultSet.getLong(1),resultSet.getString(2),resultSet.getString(3),resultSet.getDouble(4),resultSet.getLong(5),resultSet.getLong(6));
                list.add(memberType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return list;
    }

     //根据会员类型编号查询对应的会员类型
    public MemberType getById(long id){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        MemberType type = new MemberType();

        try {
            connection = DBUtil.getConnection();
            String sql="select * from membertype where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1,id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                type = new MemberType(resultSet.getLong(1),resultSet.getString(2),resultSet.getString(3),resultSet.getDouble(4),resultSet.getLong(5),resultSet.getLong(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return type;
    }
}
